#ifndef _KTV_MESSAGE_SERVER_H_
#define _KTV_MESSAGE_SERVER_H_
#include "ui_message.h"
#include <deque>
#include <map>
#include "SDL/SDL.h"
#include "SDL/SDL_thread.h"


namespace UI
{

class UI_MessageClient;


struct s_MessageClientActive
{
	s_MessageClientActive(UI_MessageClient* pC, bool bW) :
		pClient(pC), bWaitingForMessage(bW)
	{ }

	UI_MessageClient* pClient;
	bool bWaitingForMessage;
};
typedef std::multimap<unsigned char, s_MessageClientActive, std::greater<unsigned char> > t_MessageClientPriorityMap;

typedef std::map<UI::UI_Message::EMessageType, t_MessageClientPriorityMap> t_MessageClientMap;

class UI_MessageServer
{
protected:
	UI_MessageServer(void);
	virtual ~UI_MessageServer(void);
public:
	enum EClientPriority
	{
		PRIORITY_LAST = 0,  //!< The absolute lowest priority available
		PRIORITY_LOW = 50,  //!< Low priority
		PRIORITY_NORMAL = 100,  //!< Standard priority
		PRIORITY_HIGH = 150,  //!< High priority
		PRIORITY_FIRST = 255  //!< The absolute highest priority available
	};
	static UI_MessageServer& Instance(void);
	void RegisterMessageClient(UI_MessageClient* pClient, UI_Message::EMessageType eMessageType, unsigned char Priority = PRIORITY_NORMAL);
	void DeregisterMessageClient(UI_MessageClient* pClient, UI_Message::EMessageType eMessageType);
	void DeregisterMessageClient(UI_MessageClient* pClient);
	void DeliverMessage(void);
	void QueueMessage(UI_Message* pMessage);
	bool MessageAvailable(void) { return m_MessageQueue.size() > 0; }
	void IgnoreAllNewMessages(bool bIgnore) { m_bIgnoreAllNewMessages = bIgnore; }
protected:
	static UI_MessageServer* m_pInstance;
	std::deque<UI_Message*> m_MessageQueue;
	t_MessageClientMap m_MessageClients;
	bool m_bIgnoreAllNewMessages;
};

}

#endif // _KTV_MESSAGE_SERVER_H_

